<?php

namespace app\common\model;

use app\common\controller\Helper;
use think\Model;

class TieBaFile extends Model
{
    use Helper;

    protected $table = 'tie_ba_file';

    // 图片
    const TYPE_IMG = 1;
    // 视频
    const TYPE_VIDEO = 2;
    // 图片大小2M
    const SIZE_IMG = 2 * 1024 * 1024;
    // 视频大小
    const SIZE_VIDEO = 20 * 1024 * 1024;

    public static function attributes($attribute, $key = null)
    {
        $list = [
            'type' => [
                self::TYPE_IMG => '图片',
                self::TYPE_VIDEO => '视频',
            ],
        ];
        return self::attributeReturn($list, $attribute, $key);
    }

    /**
     * 获取帖子对应的文件
     * @param $ids
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getListByTie($ids)
    {
        $files = static::field('id, tie_id AS tieId, type, file,video_img')
            ->where('tie_id', 'in', $ids)
            ->select()
            ->toArray();
        $model = [];
        foreach ($files as $key => $value) {
            $value['file'] = config('file_prefix') . $value['file'];
            $value['videoImg'] = $value['video_img']?config('file_prefix') . $value['video_img']:'';
            $model[$value['tieId']][] = $value;
        }
        return $model;
    }
}